﻿using System.Net;
using System.Net.Mail;
using System.Text;

namespace H5.FIO {

    /// <summary>
    /// 提供发送邮件相关方法。
    /// </summary>
    public static class MailHelper {

        /// <summary>
        /// 向用户发送一封邮件。
        /// </summary>
        /// <param name="smtpServerName">smtp 服务器地址。</param>
        /// <param name="smtpServerUserAccount">smtp 服务器用户邮箱。</param>
        /// <param name="smtpServerPassword">smtp 服务器用户密码。</param>
        /// <param name="fromEmail">发送者的邮箱地址。</param>
        /// <param name="fromName">发送者的显示名。</param>
        /// <param name="toEmail">接受者的邮箱地址。</param>
        /// <param name="toName">收件者的显示名。</param>
        /// <param name="title">邮件主题。</param>
        /// <param name="htmlContent">内容</param>
        /// <param name="attachments">邮件附件列表。</param>
        public static void sendEmail(string smtpServerName, string smtpServerUserAccount, string smtpServerPassword, string fromEmail, string fromName, string toEmail, string toName, string title, string htmlContent, params string[] attachments) {
            using (var msg = new MailMessage(new MailAddress(fromEmail, fromName), new MailAddress(toEmail, toName))) {

                // 创建邮件内容。
                msg.Subject = title;
                msg.Body = htmlContent;
                msg.IsBodyHtml = true;
                msg.BodyEncoding = Encoding.UTF8;
                msg.Priority = MailPriority.High;

                // 添加附件。
                if (attachments != null) {
                    foreach (var attachment in attachments) {
                        msg.Attachments.Add(new Attachment(attachment));
                    }
                }

                // 发送邮件。
                using (var client = new SmtpClient()) {
                    ////client.UseDefaultCredentials = false;
                    client.Host = smtpServerName;
                    client.Credentials = new NetworkCredential(smtpServerUserAccount, smtpServerPassword);
                    client.DeliveryMethod = SmtpDeliveryMethod.Network;
                    client.Send(msg);
                }

            }
        }

        /// <summary>
        /// 向用户发送一封邮件。
        /// </summary>
        /// <param name="fromEmail">发送者的邮箱地址。</param>
        /// <param name="fromName">发送者的显示名。</param>
        /// <param name="toEmail">接受者的邮箱地址。</param>
        /// <param name="toName">收件者的显示名。</param>
        /// <param name="title">邮件主题。</param>
        /// <param name="htmlContent">内容</param>
        /// <param name="attachments">邮件附件列表。</param>
        public static void sendEmail(string fromEmail, string fromName, string toEmail, string toName, string title, string htmlContent, params string[] attachments) {
            sendEmail(ConfigManager.smtpServer, ConfigManager.smtpUser, ConfigManager.smtpPassword, fromEmail, fromName, toEmail, toName, title, htmlContent, attachments);
        }

        /// <summary>
        /// 向用户发送一封邮件。
        /// </summary>
        /// <param name="toEmail">接受者的邮箱地址。</param>
        /// <param name="toName">收件者的显示名。</param>
        /// <param name="title">邮件主题。</param>
        /// <param name="htmlContent">内容</param>
        /// <param name="attachments">邮件附件列表。</param>
        public static void sendEmail(string toEmail, string toName, string title, string htmlContent, params string[] attachments) {
            sendEmail(ConfigManager.smtpUser, ConfigManager.smtpUserName ?? ConfigManager.smtpUser, toEmail, toName, title, htmlContent, attachments);
        }

    }

}